<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-input
        placeholder="请输入内容"
        v-model="params.query"
        class="input-with-select"
        clearable
        @clear="inputClear"
      >
        <el-button
          @click="getgoodsList"
          slot="append"
          icon="el-icon-search"
        ></el-button>
      </el-input>
      <el-button type="primary" @click="goAddPage">添加商品</el-button>
      <el-table :data="goodsList" style="width: 100%" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column
          prop="goods_name"
          label="商品名称"
          width="650px"
        ></el-table-column>
        <el-table-column
          prop="goods_price"
          label="商品价格（元）"
          width="120px"
        ></el-table-column>
        <el-table-column
          prop="goods_price"
          label="商品重量"
          width="80px"
        ></el-table-column>
        <el-table-column label="创建时间">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
            ></el-button>
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="delGood(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.pagenum"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  created() {
    this.getgoodsList()
  },
  data() {
    return {
      goodsList: [],
      params: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0
    }
  },
  methods: {
    //获取商品列表
    async getgoodsList() {
      const { data: res } = await this.$http.get(`goods`, {
        params: this.params
      })
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
        return
      }
      this.total = res.data.total
      this.goodsList = res.data.goods
    },
    handleSizeChange(size) {
      this.params.pagesize = size
      this.getgoodsList()
    },
    handleCurrentChange(page) {
      this.params.pagenum = page
      this.getgoodsList()
    },
    //清空搜索框的回调函数
    inputClear() {
      this.params.query = ''
      this.getgoodsList()
    },
    //根据id删除商品
    delGood(id) {
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data: res } = await this.$http.delete(`goods/${id}`)
          if (res.meta.status !== 200) {
            this.$message.error(res.meta.msg)
            return
          }
          this.$message.success(res.meta.msg)
          this.getgoodsList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    //跳转添加商品界面
    goAddPage() {
      this.$router.push('/goods/add')
    }
  }
}
</script>
<style lang="less" scope>
.input-with-select {
  background-color: #fff;
  width: 300px !important;
  margin-right: 20px;
}
</style>
